package listener;

import org.openqa.selenium.WebDriver;
import org.testng.ITestListener;
import org.testng.ITestResult;
import utils.WebDriverUtils;


public class ScreenShotListener implements ITestListener {
    @Override
    public void onTestFailure(ITestResult result) {
        // 获取失败的测试方法信息
        String className = result.getTestClass().getRealClass().getSimpleName();
        String methodName = result.getMethod().getMethodName();
        System.out.println("Test Failed: " + className + " -> " + methodName);

        // 获取 WebDriver 实例
        Object currentClass = result.getInstance();
        WebDriver driver = ((WebDriverUtils) currentClass).driver;

        // 截图
        if (driver != null) {
            WebDriverUtils.saveScreenShot(driver, className, methodName);
        }
    }
}
